﻿using System;
using System.Linq;
using Solution.BusinessObject;
using Solution.CMS.MainDSTableAdapters;
using Solution.Utilities;

namespace Solution.CMS
{
    public partial class AlbumEditor : System.Web.UI.Page
    {
        private readonly MainDS _mainDs = new MainDS();
        private MainDS.DetailContentsRow _detailContentsRow;

        private readonly ContentsTableAdapter _contentsTableAdapter =
            new ContentsTableAdapter();

        private readonly DetailContentsTableAdapter _detailContentsTableAdapter =
            new DetailContentsTableAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            _contentsTableAdapter.Connection = CommonBO.SqlConnection;
            _contentsTableAdapter.Connection.Open();
            _detailContentsTableAdapter.Connection = CommonBO.SqlConnection;
            _detailContentsTableAdapter.Connection.Open();

            _contentsTableAdapter.Fill(_mainDs.Contents);
            _detailContentsTableAdapter.Fill(_mainDs.DetailContents);

            cboParent.DataSource =
                _mainDs
                    .Contents.Rows
                    .Cast<MainDS.ContentsRow>().ToList();
            cboParent.DataBind();

            // new
            if (Request["id"] == null)
            {
                btnSave.PostBackUrl = "albumeditor.aspx";
            }
            // edit
            else
            {
                var id = Request["id"];

                _detailContentsRow = _mainDs.DetailContents.FindById(Convert.ToInt32(id));
                if (_detailContentsRow != null)
                {
                    txtTitle.Text = _detailContentsRow.Title;
                    txtThumbnail.Text = _detailContentsRow.Thumb;
                    txtIntro.Text = _detailContentsRow.Intro;
                    txtDetail.Text = _detailContentsRow.Detail;
                    cboParent.Value = _detailContentsRow.Content_Id;
                }
                btnSave.PostBackUrl = "albumeditor.aspx?id=" + id;
            }

            _detailContentsTableAdapter.Connection.Close();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _detailContentsTableAdapter.Connection = CommonBO.SqlConnection;
                _detailContentsTableAdapter.Connection.Open();

                _detailContentsTableAdapter.Fill(_mainDs.DetailContents);

                // new
                if (Request["id"] == null)
                {
                    _detailContentsRow = (MainDS.DetailContentsRow)_mainDs.DetailContents.NewRow();
                    _mainDs.DetailContents.Rows.Add(_detailContentsRow);
                }
                // edit
                else
                {
                    var id = Request["id"];

                    _detailContentsRow = _mainDs.DetailContents.FindById(Convert.ToInt32(id));
                }

                _detailContentsRow.Title = txtTitle.Text;
                _detailContentsRow.Thumb = txtThumbnail.Text;
                _detailContentsRow.Intro = txtIntro.Text;
                _detailContentsRow.Detail = txtDetail.Text;
                if (cboParent.Value != null)
                {
                    _detailContentsRow.Content_Id = (int) cboParent.Value;
                }
                else
                {
                    _detailContentsRow.SetContent_IdNull();
                }

                _detailContentsTableAdapter.Adapter.Update(_mainDs);
                _detailContentsTableAdapter.Connection.Close();

                Response.Redirect("album.aspx");
            }
            catch (Exception exception)
            {
                Logger.WriteLog(exception);
            }
        }
    }
}